package cn.alpha.open.autocode.source;

public class MysqlDialect implements SqlDialect 
{
	private String schema;
	
	public MysqlDialect(String schema)
	{
		this.schema=schema;
	}

	@Override
	public String getTableSql() 
	{
		return "select t.TABLE_NAME as tableName ,t.TABLE_COMMENT as tableComment from information_schema.TABLES t "+
				" where t.TABLE_SCHEMA='"+schema+"'";
	}

	@Override
	public String getColumnSql(String tableName) 
	{
		return "select t.COLUMN_NAME columnName,t.ORDINAL_POSITION ordinalPosition,t.IS_NULLABLE='YES' isNullable, "+
				"t.DATA_TYPE dataType,t.NUMERIC_PRECISION `precision`, t.CHARACTER_MAXIMUM_LENGTH dataLength, "+
				"t.NUMERIC_SCALE scale,  t.COLUMN_COMMENT columnComment "+
				"from information_schema.COLUMNS t "+
				"where t.TABLE_NAME='"+tableName+"' and t.TABLE_SCHEMA='"+schema+"'";
	}

	@Override
	public String getPKSql(String tableName) 
	{
		return "SELECT k.COLUMN_NAME pkName FROM information_schema.table_constraints t  "+
				" JOIN information_schema.key_column_usage k  "+
				" USING (constraint_name,table_schema,table_name) "+
				" WHERE t.constraint_type='PRIMARY KEY'  "+
				" AND t.table_schema='"+schema+"' and t.TABLE_NAME='"+tableName+"'";
	}
	
}
